package org.yejiali.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.yejiali.common.enums.AppHttpCodeEnum;
import org.yejiali.common.response.ResponseResult;
import org.yejiali.domain.entity.LoginUser;
import org.yejiali.domain.entity.User;
import org.yejiali.mapper.UserMapper;

/**
 * @Author:yejiali
 * @Date:2025/2/18-02-18-21:48
 * @Decsription:org.yejiali.service.impl
 * @Version:1.0
 */
@Service("userDetailService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户名是否存在
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserName,username);
        User loginUser = userMapper.selectOne(lambdaQueryWrapper);
        //不存在该id,抛出异常
        if(loginUser==null){
           throw new RuntimeException("用户不存在");
        }
        //返回用户信息
        //TODO 查询权限信息封装
        return new LoginUser(loginUser);

    }
}

